from datetime import date, timedelta

from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.goods.models import GoodsVisitCount
from apps.order.models import OrderInfo
from apps.users.models import User


# /statistical/day_active/   继承一级视图
class UserDailyActiveCountView(APIView):
    # 设置权限只可以管理员访问  防止爬虫
    permission_classes = [IsAdminUser]

    def get(self, request):
        """
        日活人数数量统计
        :param request:
        :return:
        """
        # 获取当前时间
        today = date.today()
        # 如果用户最后登录的时间大于今天的0:00就说明今日登录了  计算今日所有登录过的用户的数量
        count = User.objects.filter(last_login__gte=today).count()
        # 返回数据
        return Response({'count': count})


# /statistical/day_orders/
class UserDailyOrderCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        """
        返回今日创建的订单数量
        :param request:
        :return:
        """
        # 获取今日时间
        today = date.today()
        # 查询订单表  把所有创建时间大于今日0：00的订单数量查询出来
        count = OrderInfo.objects.filter(create_time__gte=today).count()
        # 返回数据
        return Response({'count': count})


# /statistical/day_increment/
class UserDailyIncrementCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        """
        今日新增用户数量
        :param request:
        :return:
        """
        # 获取今日时间
        today = date.today()
        # 查询date_joined字段大于等于今日0:00所有用户数量
        count = User.objects.filter(date_joined__gte=today).count()

        return Response({'count': count})


class UserDailyTotalCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        count = User.objects.all().count()
        return Response({'count': count})


class UserDailyMonthIncrementCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        """
        本月新增用户数量
        :param request:
        :return:
        """
        # 定义三十天前的时间  今日时间减去30天
        start_date = date.today() - timedelta(days=31)
        # 组织前端需要的列表数据
        aList_all = []
        # 循环31次
        for i in range(32):
            # 当前时间为三十天前加上i天
            current_date = start_date + timedelta(days=i)
            # 当前时间的下一天
            next_date = start_date + timedelta(days=i + 1)
            # 每一次查询出大于等于今天时间创建的用户和小于明天时间创建的用户
            count = User.objects.filter(date_joined__gte=current_date, date_joined__lt=next_date).count()
            # 组织数据  数量和日期
            aList_all.append({
                'count': count,
                'date': current_date
            })
        # 返回数据
        return Response(aList_all)


class UserDailyGoodsDayView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        aList_all = []
        today = date.today()

        goods_visit_list = GoodsVisitCount.objects.filter(date__gte=today)

        for goods_visit in goods_visit_list:
            is_true = False
            for i in aList_all:
                if goods_visit.category.name in i.values():
                    i['count'] += goods_visit.count
                    is_true = True
            if not is_true:
                aList_all.append({
                    'category': goods_visit.category.name,
                    'count': goods_visit.count,
                })

        return Response(aList_all)
